package mb.view;

import java.awt.event.*;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;

public class AppMenu extends JMenuBar  implements ActionListener {

  private static final int EXIT			 = 1;
  private static final int SELECT		 = 2;
  private static final int DESELECT	 = 3;
  private static final int HIDE			 = 4;
  private static final int SHOW			 = 5;

  private JMenu		  menuFile	           = new JMenu("File");
  private JMenuItem menuFileExit		     = new JMenuItem("Exit");
  private JMenu 	  menuEdit 		         = new JMenu("Edit");
  private JMenuItem menuEditSelect 	     = new JMenuItem("Select Boxes");
  private JMenuItem menuEditDeselect     = new JMenuItem("Deselect Boxes");
  private JMenu 	  menuView		         = new JMenu("View");
  private JMenuItem menuViewHide 		     = new JMenuItem("Hide Selected Boxes");
  private JMenuItem menuViewShow 		     = new JMenuItem("Show Selected Boxes");

  private AppFrame appFrame;

  public AppMenu() {
  	super();
    menuFileExit.setActionCommand("Exit");
    menuFileExit.addActionListener(this);

    menuEditSelect.setActionCommand("Select");
    menuEditSelect.addActionListener(this);
    menuEditDeselect.setActionCommand("Deselect");
    menuEditDeselect.addActionListener(this);

    menuViewHide.setActionCommand("Hide");
    menuViewHide.addActionListener(this);
    menuViewShow.setActionCommand("Show");
    menuViewShow.addActionListener(this);

    menuFile.add(menuFileExit);
    menuEdit.add(menuEditSelect);
    menuEdit.add(menuEditDeselect);
    menuView.add(menuViewHide);
    menuView.add(menuViewShow);

    this.add(menuFile);
    this.add(menuEdit);
    this.add(menuView);
	}

  public AppMenu(AppFrame aFrame) {
    this();
    this.setAppFrame(aFrame);
  }

  public AppFrame getAppFrame() {
  	return appFrame;
  }

  public void setAppFrame(AppFrame aFrame) {
  	appFrame = aFrame;
  }

  private int getCommand(String aCommand) {  // &
    int	action = -1;
    if      (aCommand.equals("Exit"))     action = EXIT;
    else if (aCommand.equals("Select"))   action = SELECT;
    else if (aCommand.equals("Deselect")) action = DESELECT;
    else if (aCommand.equals("Hide"))     action = HIDE;
    else if (aCommand.equals("Show"))     action = SHOW;
    return action;
  }

  public void actionPerformed(ActionEvent e) {
		String command = e.getActionCommand();
    switch (this.getCommand(command)) {  
    	case EXIT:
        appFrame.exit();
        break;
    	case SELECT:
        appFrame.getDiagram().selectAllBoxes();
        break;
    	case DESELECT:
        appFrame.getDiagram().deSelectAllBoxes();
        break;
    	case HIDE:
        appFrame.getDiagram().hideSelectedBoxes();
        break;
    	case SHOW:
        appFrame.getDiagram().showSelectedBoxes();
        break;
    }
  }

}
